package com.esaheki.flashcards.servlets;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.esaheki.flashcards.classes.*;
import com.esaheki.flashcards.dao.*;
import com.esaheki.flashcards.support.*;

public class UpdateScore extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7323844603436216772L;
	
	private static final String DEFAULT_PATH = "http://localhost:8080/WebFlashCards";

	public void service(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String type = request.getParameter("type");
		String id = request.getParameter("id");
		String cid = request.getParameter("cid");
		String inc = request.getParameter("inc");

		try {
			CardDAO cardDao = new CardDAO();
			
			Card card = cardDao.getCardById(Integer.parseInt(cid));
			card.setCountTests(card.getCountTests() + 1);

			if (inc.equals("1")) {
				card.setCountRight(card.getCountRight() + 1);
			}
			cardDao.updateCard(card);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Statistic st = new Statistic();
		st.updateScore();

		response.setCharacterEncoding("UTF-8");
		response.sendRedirect(DEFAULT_PATH + "/test.jsp?type=" + type + "&id=" + id + "&answer=no");
	}

}